package com.tianqu.tms.www.transfer.action;

import com.tianqu.LicensePlateConfigConstant;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.www.transfer.actionform.TransferRgActionForm;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

public class TransferSeniorSearchResultAction extends TmsAction {
	private Logger logger = Logger.getLogger(TransferSeniorSearchResultAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TransferRgActionForm inForm = null;
		if(uc.getAttribute("seniorFrom")!=null)
		{
			inForm = (TransferRgActionForm)uc.getAttribute("seniorFrom");
		}else{
			inForm = (TransferRgActionForm) form;
		}
		
		TransferRgInputViewHelper view = new TransferRgInputViewHelper();
		if((inForm.getSerial()==null||"".equals(inForm.getSerial()))
				&& (inForm.getVin()==null||"".equals(inForm.getVin()))
				&& (inForm.getLicensePlate()==null||"".equals(inForm.getLicensePlate())|| LicensePlateConfigConstant.defLicensePlate.equals(inForm.getLicensePlate())))
		{
			setErrorsToRequest(request, TmsConstants.U0040, null);
			return mapping.getInputForward();
		}
		//根据流水号或车牌号码进行搜索判断存不存在
		TransferAcceptedInfo accept = new TransferAcceptedInfo();
		String licensePlate=inForm.getLicensePlate();
		if(LicensePlateConfigConstant.defLicensePlate.equals(inForm.getLicensePlate()))
		{
			licensePlate = "";
		}
		accept = accept.getAcceptInfoBySerialAndCarLicense(inForm.getSerial(),licensePlate, 0);
		if(accept!=null) {
			//存在则开始查询 load信息 seniorList
			List<TransferAcceptedInfo> list = accept.getAcceptInfoBySerialAndLicense(inForm.getSerial(),licensePlate, inForm.getVin());
			//获取车辆类型
			List<TransferRgInputViewHelper> seniorList = null;
			Car car = new Car();
			TransferPersonalCarInfo person = new TransferPersonalCarInfo();
			Codes code = Codes.getInstance();
			if(list!=null) {
				seniorList = new ArrayList<TransferRgInputViewHelper>();
				for(int i=0;i<list.size();i++)
				{
					TransferAcceptedInfo entity = list.get(i);
					if(entity == null) continue;
					TransferRgInputViewHelper vv = new TransferRgInputViewHelper();
					if(entity.getMarketType() == 1)
					{
						//库存
						car = car.findCarBySerial(entity.getBasicCarId());
						if(car!=null)
						{
							vv.setCarType(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE, car.getCarType())); 
						}
					}
					if(entity.getMarketType() == 2)
					{
						//个人
						person = person.getPersonalCarInfoBySerial(entity.getBasicCarId());
						if(person!=null)
						{
							vv.setCarType(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE, person.getCarType())); 
						}
					}
					//序号 流水号 车牌号码 VIN码 车主姓名  
					vv.setStatus(String.valueOf(entity.getStatus()));
					vv.setStatusName(code.getItemValueByItemIdAndItemCode(Codes.ACCEPT_STATUS, entity.getStatus())); 
					vv.setAcceptedTime(entity.getInsertTimeS());
					vv.setCarOwner(entity.getCarOwner());
					vv.setVin(entity.getVin());
					vv.setLicensePlate(entity.getLicensePlate());
					vv.setSerial(entity.getSerial());
					vv.setNum(i+1);
					if(i%2 == 0){
						vv.setCssFlag(1);
					}else{
						vv.setCssFlag(2);
					}
					seniorList.add(vv);
				}
			}
			view.setSeniorList(seniorList);
		}else{
			setErrorsToRequest(request, TmsConstants.IM006, null);
			return mapping.getInputForward();				
		}
//		uc.setAttribute("seniorFrom", inForm);
		request.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
